/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.settings.panel;

import javax.swing.event.DocumentEvent;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;

/**
 * The form settings panel.
 */
@SuppressWarnings("serial")
public class FormSettingsPanel
    extends AbstractFormSettingsPanel
{

  /**
   * The form model.
   */
  protected FormModel model = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          GUI. The gui.
   */
  public FormSettingsPanel(FormEditorGUI gui)
  {
    super(gui);
  }



  /**
   * Set the form.
   * 
   * @param form
   *          the form
   */
  public void setElement(FormModel form)
  {
    this.getNameTextField().getDocument().removeDocumentListener(this);
    this.model = form;
    if (this.model != null) {
      this.getNameTextField().setText(this.model.getName());
      this.getTypeTextField().setText(this.model.getType().toString());
      if (this.model.getType() != null && FormType.LABELED.equals(this.model.getType())) {
        this.getKeyLabel().setVisible(true);
        this.getKeyTextField().setVisible(true);
        this.getKeyTextField().setText(this.model.getLabelKey());
      }
      else {
        this.getKeyLabel().setVisible(false);
        this.getKeyTextField().setVisible(false);
        this.getKeyTextField().setText("");
      }
    }
    else {
      this.getKeyLabel().setVisible(false);
      this.getKeyTextField().setVisible(false);
      this.getNameTextField().setText("");
      this.getTypeTextField().setText("");
      this.getKeyTextField().setText("");
    }
    this.getNameTextField().getDocument().addDocumentListener(this);
    this.validateSettings();
  }



  /**
   * Get the form.
   * 
   * @return the form
   */
  public FormModel getElement()
  {
    if (this.model != null) {
      this.model.setName(this.getNameTextField().getText());
      if (this.model.getType() != null && FormType.LABELED.equals(this.model.getType())) {
        this.model.setLabelKey(this.getKeyTextField().getText());
      }
    }
    return this.model;
  }



  /**
   * Handle the document changed.
   * 
   * @param e
   *          the document event.
   */
  public void changedUpdate(DocumentEvent e)
  {
    // Do nothing
  }



  /**
   * Handle the document insert changed.
   * 
   * @param e
   *          the document event.
   */
  public void insertUpdate(DocumentEvent e)
  {
    this.model = this.getElement();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.model);
    this.validateSettings();
  }



  /**
   * Handle the document remove changed.
   * 
   * @param e
   *          the document event.
   */
  public void removeUpdate(DocumentEvent e)
  {
    this.model = this.getElement();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyElementSettingsChanged(this.model);
    this.validateSettings();
  }



  /**
   * Check whether the settings are valid.
   */
  public void validateSettings()
  {
    if (this.getNameTextField().getText() == null || this.getNameTextField().getText().trim().length() == 0) {
      this.getNameLabel().setForeground(AbstractGUI.COLOR_TEXT_ERROR);
    }
    else {
      this.getNameLabel().setForeground(AbstractGUI.COLOR_TEXT);
    }

    if (this.model.getType() != null && FormType.LABELED.equals(this.model.getType())) {
      if (this.getKeyTextField().getText() == null || this.getKeyTextField().getText().trim().length() == 0) {
        this.getKeyLabel().setForeground(AbstractGUI.COLOR_TEXT_ERROR);
      }
      else {
        this.getKeyLabel().setForeground(AbstractGUI.COLOR_TEXT);
      }
    }
  }

}
